import axios from 'axios';
//自动切换环境
axios.defaults.baseURL = process.env.API_ROOT
    //设置超时时间
axios.defaults.timeout = 10000;
// post请求头
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'
axios.defaults.withcredentials = true //允许跨域携带cookie信息
    //请求拦截(请求发出前处理请求)
    axios.interceptors.request.use((config) => {
        layer.load(0, {
            shade: false,
        }); //0代表加载的风格，支持0-2
        //在发送请求之前如果为post序列化请求参数
        if (config.method === 'post') {
            config.data = config.data;
        }
        return config;
    }, (error) => {
        return Promise.reject(error);
    });
    // 响应拦截器（处理响应数据）
    axios.interceptors.response.use((res) => {
        //对响应数据做判断，与后台协议统一接口返回格式
        //  console.log('>>>>>>>response: ', res);
        // console.log(res.status)
        // if (res.data.Code != 1) { //这个判断可根据实际情况修改
        // return Promise.reject(res);
        // }
        layer.closeAll();
        if (res.status != 200) { //这个判断可根据实
            layer.closeAll();
            return Promise.reject(res);
        }
        return res;
    }, (error) => {
        layer.closeAll();
        return Promise.reject(error);
    });
// 封装get方法
function get(url, params) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params,
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
                "Accept": "application/json",
                "Content-Type": "application/json; charset=UTF-8",
                'token': '716f853456467559fc1cb1238a878828',
                'Member': 'limeimei',
                'Language': 'zh-cn',
                'Gender': '1',
                'PayType': 4,
                'Version': '-1'
            }
        }, ).then(res => {
            resolve(res.data);
        }).catch(err => {
            reject(err.data);
        })
    });
}
// 封装post方法
// 'Content-Type': 'application/x-www-form-urlencoded'
//"Content-type", "multipart/form-data
//application/json;charset=UTF-8
function post(url, params) {
    return new Promise((resolve, reject) => {
        axios.post(url, params).then(res => {
            resolve(res.data);
        }).catch(err => {
            reject(err.data);
        })
    });
}
//form表单
function post2(url, params) {
    return new Promise((resolve, reject) => {
        axios.post(url, null, {
            params: params,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
                'token': '716f853456467559fc1cb1238a878828',
                'Member': 'limeimei',
                'Language': 'zh-cn',
                'Gender': '1',
                'PayType': 4,
                'Version': '-1'
            }
        }).then(res => {
            resolve(res.data);
        }).catch(err => {
            reject(err.data);
        })
    });
}
//post 4
function post4(url, params) {
    return new Promise((resolve, reject) => {
        axios.post(url, params, {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
                'token': '716f853456467559fc1cb1238a878828',
                'Member': 'limeimei',
                'Language': 'zh-cn',
                'Gender': '1',
                'PayType': 4,
                'Version': '-1'
            }
        }).then(res => {
            resolve(res.data);
        }).catch(err => {
            reject(err.data);
        })
    });
}
//上传文件
function post3(url, params) {
    return new Promise((resolve, reject) => {
        axios.post(url, params, {
            headers: {
                'Content-Type': 'multipart/form-data;charset=UTF-8'
            }
        }).then(res => {
            resolve(res.data);
        }).catch(err => {
            reject(err.data);
        })
    });
}
//对外接口
export function request({ method, url, data, re_header }) {
    if (method == 'get') {
        return get(url, data);
    } else if (method == 'post') {
        if (re_header == 1) {
            return post2(url, data);
        } else if (re_header == 2) {
            return post3(url, data);
        } else if (re_header == 4) {
            return post4(url, data);
        } else {
            return post(url, data);
        }
    }
}

export default {
    install(Vue) {
        Vue.prototype.$axios = axios;
        Vue.prototype.$request = function() {
            return request;
        }
    }
}